package com.dys.flex;

import com.dys.entity.SysUser;
import com.dys.mapper.SysUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.dys.entity.table.SysUserTableDef.SYS_USER;

/**
 * @Author: 疾风
 * @ClassName: QueryData
 * @Description: 查询
 * @CreateDate: 2025/01/14
 * @Version: v1.0.0
 */
@SpringBootTest
public class QueryData {

    @Autowired
    private SysUserMapper userMapper;

    @Test
    void testSelectOne(){
        SysUser user = userMapper.selectOneById(21);
        System.out.println(user);
    }

    @Test
    void testSelectOneByMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("id",21);
        SysUser user = userMapper.selectOneByMap(map);
        System.out.println(user);
    }

    @Test
    void testSelectOneByCondition(){
        SysUser user = userMapper.selectOneByCondition(SYS_USER.ID.eq(1));
        System.out.println(user);
    }

    @Test
    void testSelectOneByQuery(){
        // 有多条数据的时候,会报错
        SysUser user = userMapper.selectOneByQuery(QueryWrapper.create().where(SYS_USER.ID.eq(1)));
        System.out.println(user);
    }

    @Test
    void testSelectList(){
        List<SysUser> userList = userMapper.selectListByQuery(QueryWrapper.create().where(SYS_USER.ID.ge(2)));
        userList.forEach(System.out::println);
    }

    @Test
    void testSelectObject(){
        Object object = userMapper.selectObjectByQuery(QueryWrapper.create()
                .select(SYS_USER.NICK_NAME,SYS_USER.USER_NAME).where(SYS_USER.ID.eq(2)));
        System.out.println(object);
    }

    @Test
    void  testSelectObjectListByQuery(){
        List<Object> list = userMapper.selectObjectListByQuery(QueryWrapper.create().select(SYS_USER.USER_NAME).where(SYS_USER.ID.ge(2)));
        list.forEach(System.out::println);
    }

    @Test
    void testSelectByMap(){
        List<Row> rows = userMapper.selectRowsByQuery(QueryWrapper.create().where(SYS_USER.ID.ge(1)));
        rows.forEach(System.out::println);
        System.out.println("----------------------------------------");
        Row row = rows.get(0);
        System.out.println(row);
        System.out.println("----------------------------------------");
        Integer status = row.getInt("id");
        System.out.println(status);
    }
}
